package com.leyu.mall.admin.goods.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.leyu.mall.admin.cms.service.ICmsChannelService;
import com.leyu.mall.admin.goods.domain.GoodsBase;
import com.leyu.mall.admin.goods.domain.GoodsCategory;
import com.leyu.mall.admin.goods.domain.GoodsRelation;
import com.leyu.mall.admin.goods.service.IGoodsBaseService;
import com.leyu.mall.admin.goods.service.IGoodsBrandService;
import com.leyu.mall.admin.goods.service.IGoodsCategoryService;
import com.leyu.mall.admin.goods.service.IGoodsRelationService;
/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 * @author OOKK
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@Scope(value = "prototype")
@RequestMapping("admin/goods")
public class GoodsBaseController
{
	@Resource(name = "goodsBaseServiceImpl")
	private IGoodsBaseService		goodsBaseServiceImpl;
	@Autowired
	private IGoodsBrandService		goodsBrandServiceImpl;
	@Autowired
	private IGoodsCategoryService	goodsCategoryService;
	@Autowired
	private ICmsChannelService		cmsChannelService;

	@Autowired
	private IGoodsRelationService goodsRelationServiceImpl;
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public String save(Model model, GoodsBase goodsBase )
	{
		try
		{
			
//			System.out.println("GoodsBaseController:save1"+relationIds);
			System.out.println("GoodsBaseController:save2"+goodsBase.getRelationId());
			String relationids [] = goodsBase.getRelationId().split(",");
			System.out.println("GoodsBaseController:relationids"+relationids);
			
			 
			List<GoodsRelation> grlist = new ArrayList();
			
			for(int i = 0 ; i < relationids.length; i ++){
				GoodsRelation goodsRelation = new GoodsRelation();
				goodsRelation.setGoodsId(goodsBase.getGoodsId());
				goodsRelation.setReGoodsId(Long.parseLong(relationids[i]));
				goodsRelation.setGoodsType(goodsBase.getGoodsType());
				
				grlist.add(goodsRelation);
			}
			
			
			goodsRelationServiceImpl.save(grlist);
			goodsBaseServiceImpl.save(goodsBase);
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../goods";
	}

	@RequestMapping(value = "delete/{goodsID}", method = RequestMethod.GET)
	public String delete(Model model, @PathVariable long goodsID)
	{
		try
		{
			goodsBaseServiceImpl.deleteGoodsBase(goodsID);
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:../../goods";
	}

	@RequestMapping(value = "edit/{goodsID}", method = RequestMethod.GET)
	public String edit(Model model, @PathVariable long goodsID)
	{
		try
		{
			model.addAttribute("entity", goodsBaseServiceImpl.getGoodsBaseById(goodsID));
			model.addAttribute("brandlist", goodsBrandServiceImpl.getAllDisplayGoodsBrand());
			model.addAttribute("categorylist", goodsCategoryService.getAllDisplayCategoryList());
			model.addAttribute("channellist", cmsChannelService.getcmsChannelList());
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/editgoods.html";
	}

	@ExceptionHandler(Exception.class)
	public String exception(Exception e, HttpServletRequest request)
	{
		request.setAttribute("exception", e);
		return "/error.html";
	}

	@RequestMapping(value = "new", method = RequestMethod.GET)
	public String newBrand(Model model,@RequestParam(value = "curPage", required = false, defaultValue ="1" ) int curPage) throws Exception
	{
		System.out.println("---------------");
		model.addAttribute("brandlist", goodsBrandServiceImpl.getAllDisplayGoodsBrand());
		List<GoodsCategory> categorylist = goodsCategoryService.getAllDisplayCategoryList();
		model.addAttribute("categorylist", categorylist);
		model.addAttribute("channellist", cmsChannelService.getcmsChannelList());
		
		List<GoodsBase> goodslist = goodsBaseServiceImpl.getAllGoodsBase();
		model.addAttribute("goodslist", goodslist);
//		 
	
		
//		List twodepthList = new ArrayList();
		Map datamap = new HashMap();
//		for(GoodsCategory cate : categorylist){
			 
			for(GoodsBase goods : goodslist ){
				if(!datamap.containsKey(goods.getCategoryId())){
					List datalist = new ArrayList();
					datalist.add(goods);
					datamap.put(goods.getCategoryId(), datalist);
				}else  {
					
					List oldlist = (List)datamap.get(goods.getCategoryId());
					oldlist.add(goods);
					datamap.put(goods.getCategoryId(), oldlist);
				}
				 
//				if(datamap.get(cate)!=null){
//					 
//					 List dalist = (List) datamap.get(cate);
//					 dalist.add(goods);
//					 datamap.put(cate, dalist);
//				}else{
//					List datalist = new ArrayList();
//					datalist.add(goods);
//				 datamap.put(cate, datalist);
//				}
//				}
//			}
		}
		
		model.addAttribute("datamap", datamap);
		
		GoodsBase pagebean = new GoodsBase();
		int totalItem = goodsBaseServiceImpl.getGoodsListByPageCount();
		pagebean.setTotalItem(totalItem);
		pagebean.setCurPage(curPage);
		model.addAttribute("goodslist", goodsBaseServiceImpl.getGoodsListByPage(pagebean));
//		List<GoodsBase> goodslist = goodsBaseServiceImpl.getGoodsListByPage(pagebean);
		model.addAttribute("pagebean", pagebean);
		
		return "admin/newgoods.html";
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String index(Model model,@RequestParam(value = "curPage", required = false, defaultValue ="1" ) int curPage) throws Exception
	{
		try
		{
			GoodsBase pagebean = new GoodsBase();
			int totalItem = goodsBaseServiceImpl.getGoodsListByPageCount();
			pagebean.setTotalItem(totalItem);
			pagebean.setCurPage(curPage);
			model.addAttribute("goodslist", goodsBaseServiceImpl.getGoodsListByPage(pagebean));
			model.addAttribute("pagebean", pagebean);
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "admin/goods.html";
	}
}
